package com.qcu.controller.user;

import com.qcu.annotation.RequireAuthorization;
import com.qcu.entity.Report;
import com.qcu.exception.RepeatException;
import com.qcu.service.InhabitantService;
import com.qcu.service.ReportService;
import com.qcu.util.Response;
import com.qcu.util.ResponseCode;
import com.qcu.util.ThreadLocalUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @Name: MyComputer
 * @ClassName ReportController
 * @Description:
 * @date 2025/2/8 9:56
 */
@RestController
@RequestMapping("/user/inhabitant/report")
public class ReportController {

    @Resource
    private InhabitantService inhabitantService;
    @Resource
    private ReportService reportService;

    /**
     * 创建报告
     * @param report
     * @return
     */
    @PostMapping("/create")
    @RequireAuthorization({"USER"})
    public Response create(@RequestBody Report report)throws RepeatException {
        try {
            Map<String,Object> o = ThreadLocalUtil.get();
            Integer o1 = (Integer) o.get("id");
            report.setUserId(o1);
            boolean save = reportService.save(report);
            return save ? Response.success() : Response.failure(ResponseCode.FAILURE, "创建失败");
        } catch (Exception e) {
            throw new RepeatException(e.getMessage());
        }
    }
    @PostMapping("/suggestion")
    @RequireAuthorization({"USER"})
    public Response suggestion(@RequestBody Report report)throws RepeatException {
        BigDecimal temperatureLowerBound = new BigDecimal("36.0");
        BigDecimal temperatureUpperBound = new BigDecimal("37.5");
        String res = "";
        if (report.getFrequency() > 120 || report.getFrequency() < 60) {
            res += "心率异常，请注意以下事项： 及时就医，尤其是伴随有胸痛、呼吸困难、头晕等症状时。 记录下心率异常发生的时间、频率以及任何伴随的症状。 减少或避免摄入咖啡因、尼古丁等可能引起心率变化的物质。 保持健康的生活方式，包括规律运动、均衡饮食、充足睡眠和减轻压力。";
        }
        if (report.getTemperature().compareTo(temperatureLowerBound) < 0 || report.getTemperature().compareTo(temperatureUpperBound) > 0) {
            res += "体温异常：尽量待在阴凉处，避免高温环境。 如果伴有发热、头痛、乏力等症状，请及时就医。";

        }

        return Response.success(res);
    }

}
